<?

//Data loading
if (GetParam("office","")) $perush = GetCompany (GetParam("office",""));
else $perush = 'PT. MASAJI PRAYASA CARGO';

include 'ais_report_soa_payable_functions.php';
$pdf=new PDFReport();
//$pdf->setPiutang(GridListPiutang());
$debtur = GetParam("debturCode","");
if($debtur){
  $pdf->setDebtur($debtur);
  $pdf->setGrid(GridList($debtur));
  $pdf->Exec();
}else{
  $SQL = "SELECT DISTINCT debtur FROM view_soa_payable
          WHERE MONTH(tglbkt) = '".GetParam('searchMonth','')."' AND
          YEAR(tglbkt) = '".GetParam('searchYear','')."'
          ORDER BY debtur"; //die($SQL);
  $query = mysql_query($SQL);
  if($query && mysql_num_rows($query) > 0){
    while($row = mysql_fetch_object($query)){
      $pdf->setDebtur($row->debtur);
      $pdf->setGrid(GridList($row->debtur));
      $pdf->Exec();
    }
  }
}
$pdf->Output();


class PDFReport extends FPDF {
    var $widths = array(20,16,53,28,5,28,15,25);
    var $data = NULL;
    var $curr = 'IDR';
    var $debtur_code = '';

    function PDFReport(){
        parent::FPDF('P','mm','A4');
        $this->AddFont('arialn','','arialn.php');
        $this->SetFont('arialn','',9);
        $this->curr = GetParam("curr","");
    }

    function setDebtur($debtur_code){
        $this->debtur_code = $debtur_code;
    }

    function Header(){
        global $perush,$DBConnection;
        $this->Cell(190,5,$perush,0,1,'C');
        $this->Cell(190,5,'STATEMENT OF ACCOUNT PAYABLE DETAIL',0,1,'C');
        $arrMonth = array('','Jan','Feb','Mar','Apr','May','Jun','Jul','Aug','Sep','Oct','Nov','Dec');
        $this->Cell(190,5,'AS AT : '.strtoupper($arrMonth[GetParam("searchMonth","")]).' '.GetParam('searchYear',''),0,1,'C');
        $this->Ln();

        $SQLDeb = "SELECT CUNAME FROM tbldebtur WHERE CUCODE = '".$this->debtur_code."'";
        $result = $DBConnection->dbc->get_row($SQLDeb);

        $this->Cell(120,5,$result->CUNAME);
        $this->Cell(70,5,'PAGE : '.$this->PageNo(),0,1);
        $this->Cell(120,5,'');
        $this->Cell(70,5,'CUST NO : '.$this->debtur_code,0,1);
        $this->Cell(120,5,'');
        $this->Cell(70,5,'CURRENCY : '.$this->curr,0,1);
        $this->Ln();
        $this->Cell($this->widths[0],8,'REFF',1,0,'C');
        $this->Cell($this->widths[1],8,'DATE',1,0,'C');
        $this->Cell($this->widths[2],8,'DESCRIPTION',1,0,'C');
        $this->Cell($this->widths[3],8,'AMOUNT (ORG)',1,0,'C');
        $this->Cell($this->widths[4],8,'D/C',1,0,'C');
        $this->Cell($this->widths[5],8,'AMOUNT (RPH)',1,0,'C');
        $this->Cell($this->widths[6],8,'ACCOUNT',1,0,'C');
        $this->Cell($this->widths[7],8,'REFERENCE',1,1,'C');
    }

    function setGrid($data){
        $this->data = $data;
    }

    function Exec(){
        $this->AddPage();
        $balance = getBeginningBalance($this->debtur_code);

        $this->Cell($this->widths[0],5,'',1,0,'C');
        $this->Cell($this->widths[1],5,formatDate($balance->LAST_DATE),1,0,'C');
        $this->Cell($this->widths[2],5,'BEGINNING BALANCE',1,0);

        $amount = $balance->total_org;
        //$jmlStr = $amount < 0 ? '('.$amount.')' : $amount;
        $flag = $amount < 0 ? 'Cr' : 'Dr';

        $this->Cell($this->widths[3],5,FormatNumber($balance->total_org,2),1,0,'R');
        $this->Cell($this->widths[4],5,$flag,1,0,'C');
        $this->Cell($this->widths[5],5,FormatNumber($balance->total_rph,2),1,0,'R');
        $this->Cell($this->widths[6],5,'',1,0,'R');
        $this->Cell($this->widths[7],5,'',1,1);

        if($this->data){
          foreach($this->data as $row){
            $this->Cell($this->widths[0],5,$row['nobkt'],1,0,'C');
            $this->Cell($this->widths[1],5,formatDate($row['tglbkt']),1,0,'C');
            $this->Cell($this->widths[2],5,$row['uraian'],1,0);

            if($row['dk'] == 'D'){
              $flag = 'Dr';
              //$jmlStr = $row['jumlah'];
              //$rphStr = $row['rupiah'];
            }else{
              $flag = 'Cr';
/*
              $row['jumlah'] *= -1;
              $row['rupiah'] *= -1;
*/
              //$jmlStr = '('.$row['jumlah'].')';
              //$rphStr = '('.$row['rupiah'].')';
            }

            $this->Cell($this->widths[3],5,FormatNumber($row['jumlah'],2),1,0,'R');
            $this->Cell($this->widths[4],5,$flag,1,0,'C');
            $this->Cell($this->widths[5],5,FormatNumber($row['rupiah'],2),1,0,'R');
            $this->Cell($this->widths[6],5,$row['skada'],1,0,'R');
            $this->Cell($this->widths[7],5,$row['noref'],1,1);
          }
        }
        $total = getTotal($this->debtur_code);
        $org = $total['total_org'];
        $rph = $total['total_rph'];
        //$strTotalORG = ($total['total_org'] < 0) ? '('.abs($total['total_org']).')' : sprintf('%.2f',$total['total_org']);
        //if($totalORG == 0) $totalRPH = 0;
        //$strTotalRPH = ($total['total_rph'] < 0) ? '('.abs($total['total_rph']).')' : sprintf('%.2f',$total['total_rph']);

        $this->Cell($this->widths[0],7,'',1,0);
        $this->Cell($this->widths[1],7,'',1,0);
        $this->Cell($this->widths[2],7,'BALANCE DUE TO YOU',1,0);
        $this->Cell($this->widths[3],7,FormatNumber($org,2),1,0,'R');
        $this->Cell($this->widths[4],7,'',1,0);
        $this->Cell($this->widths[5],7,FormatNumber($rph,2),1,0,'R');
        $this->Cell($this->widths[6],7,'',1,0);
        $this->Cell($this->widths[7],7,'',1,1);
    }

}


?>
